package com.souku.finance.application.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.ExperienceAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.ExperienceListBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/21.
 */

public class ExperienceListActivity extends BaseActivity implements View.OnClickListener {

    private List<ExperienceListBean.ListBean> list = new ArrayList<>();
    private ExperienceAdapter adapter;
    protected PullToRefreshListView pullToRefresh;
    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ImageView topMore;
    protected RelativeLayout idLlTop;
    private int page = 1;

    @Override
    public void internet() {
        pullToRefresh.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pullToRefresh.setRefreshing(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_experiencelist);
        initView();
        topTitle.setText("体验金列表");
        topMore.setVisibility(View.VISIBLE);
        pullToRefresh.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new ExperienceAdapter(list, new ExperienceAdapter.Click() {
            @Override
            public void use(String id) {
                use_exp_money(id);
            }
        });
        pullToRefresh.setAdapter(adapter);
        pullToRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                list.clear();
                page = 1;
                getList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getList();
            }
        });
    }

    private void initView() {
        pullToRefresh = (PullToRefreshListView) findViewById(R.id.pull_to_refresh);
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(ExperienceListActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        topMore = (ImageView) findViewById(R.id.top_more);
        topMore.setImageResource(R.drawable.exp_help);
        topMore.setOnClickListener(ExperienceListActivity.this);
        idLlTop = (RelativeLayout) findViewById(R.id.id_ll_top);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.top_more) {
            Intent intent = new Intent(this, ExperienceHelp.class);
            startActivity(intent);
        }
    }


    private void getList() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_size", "10");
        map.put("pg_no", page + "");
        RequestManager.getInstance().post(UrlConstact.MONEY_LIST, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                pullToRefresh.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    ExperienceListBean bean = JsonUtil.fastBean(response, ExperienceListBean.class);
//                    list = new JsonUtil().fastArrayBean(response, ExperienceListBean.ListBean.class);
                    if (bean.getList().size() == 0) {
//                    if (list.size() == 0) {
                        ToastUtil.show("没有更多内容");
                    } else {
                        list.addAll(bean.getList());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                pullToRefresh.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }

    private void use_exp_money(String id) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("emid", id);
        RequestManager.getInstance().post(UrlConstact.USE_EXP_MONEY, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("使用成功");
                    pullToRefresh.setRefreshing(true);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }
}
